#!/bin/bash

set -eux

### --start_docs
## Prepare images for deploying the overcloud
## ==========================================

## Prepare Your Environment
## ------------------------

## * Source in the undercloud credentials.
## ::

source {{ working_dir }}/stackrc

## * Get nodes UUID
## ::

export ironic_nodes="$( openstack baremetal node list | awk '/power/ {print $2}' )"

for ironic_node in $ironic_nodes; do
    # extract IP from ironic node details
    ip_address=$(openstack baremetal node show -f value -c driver_info $ironic_node | sed -n "s/.*ipmi_address': u'\([0-9\.]*\)'.*/\1/p")

    # get information for the matching template
    {% for node in root_device_hints %}
        NODE_IP="{{ node['ip'] }}"
        if [ "$NODE_IP" == "$ip_address" ]; then
            # set property
            openstack baremetal node set --property root_device='{"{{ node['key'] }}": "{{ node['value'] }}"}' $ironic_node
        fi
    {% endfor %}
done

### --stop_docs
